perf(db): eliminate closure allocation in getMemTables cleanup#2291
Open
shaunpatterson wants to merge 1 commit into
Open
perf(db): eliminate closure allocation in getMemTables cleanup#2291shaunpatterson wants to merge 1 commit into
shaunpatterson wants to merge 1 commit into
Conversation
getMemTables previously returned (tables, cleanupFunc) where cleanupFunc was a closure capturing the tables slice. That closure escaped to the heap on every call (one extra allocation per iterator construction in the rollup path). Split into: func (db *DB) getMemTables() []*memTable func decrMemTables([]*memTable) Callers do `defer decrMemTables(tables)` after the getMemTables call. The slice is fully populated before the defer evaluates its argument, so the captured slice header points to the correct backing array; no behavioral change. decrMemTables is a free function (no receiver) so the deferred call record doesn't pay for a *DB pointer alongside the slice header. Saves 1 alloc and 32 B per iterator construction on BenchmarkRollupKeyIterator: before: 801 B/op, 14 allocs/op after: 769 B/op, 13 allocs/op Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
getMemTablespreviously returned(tables, cleanupFunc)wherecleanupFuncwas a closure capturing thetablesslice. That closure escaped to the heap on every call — one extra allocation per iterator construction in the rollup path.Split into:
Callers do
defer decrMemTables(tables)after thegetMemTablescall. The slice is fully populated before the defer evaluates its argument, so the captured slice header points to the correct backing array; no behavioral change.decrMemTablesis a free function (no receiver) so the deferred call record doesn't pay for a*DBpointer alongside the slice header.Benchmark — BenchmarkRollupKeyIterator
Test plan
go test ./...passesdb.get,txn.NewIterator,stream_writer) verified to release refs via defer🤖 Generated with Claude Code